package org.tigris.atlas.xml.schema.mda.element;

import org.tigris.atlas.mda.metadata.MetadataRepository;
import org.tigris.atlas.mda.metadata.element.Entity;
import org.tigris.atlas.mda.metadata.element.Enumeration;
import org.tigris.atlas.mda.metadata.element.Parameter;

public class SchemaParameter implements Parameter {

	private Parameter parameter;

	public SchemaParameter(Parameter parameterToDecorate) {
		parameter = parameterToDecorate;
	}

	public String getName() {
		return parameter.getName();
	}
	
	public String getDocumentation() {
		return parameter.getDocumentation();
	}

	public String getType() {
		return parameter.getType();
	}

	public String getProject() {
		return parameter.getProject();
	}

	//schema-specific generation methods:


	public boolean isEntity() {
		return getEntity() != null;
	}

	public SchemaEntity getEntity() {
		Entity e = MetadataRepository.getInstance().getEntity( getType() );

		if( e == null ) {
			return null;
		}

		return new SchemaEntity( e );
	}

	public SchemaEnumeration getEnumeration() {
		Enumeration e = MetadataRepository.getInstance().getEnumeration( getType() );

		if( e == null ) {
			return null;
		}

		return new SchemaEnumeration( e );
	}


	/**
	 * Return namespace name of this instance (ie, the 'xs' part of 'xmlns:xs')
	 */
	public String getNamespaceName() {
		String name;
		SchemaEntity entity = getEntity();
		SchemaEnumeration enumeration = getEnumeration();

		if ( entity != null ) {
			name = entity.getNamespaceName();
		}
		else if( enumeration != null ) {
			name = enumeration.getNamespaceLocation();
		}
		else {
			name = "xs";
		}
		return name;
	}

	public String getSchemaType() {
		String type;
		SchemaEntity entity = getEntity();
		SchemaEnumeration enumeration = getEnumeration();

		if ( entity != null ) {
			type = entity.getSchemaType() + "Type";
		}
		else if( enumeration != null ) {
			type = enumeration.getSchemaType() + "Type";
		}
		else {
			type = SchemaElementUtils.getSchemaType(getType());
		}
		return type;


	}

	public boolean isMany() {
		return parameter.isMany();
	}

}
